﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VietImage.Models;
using VietImage.Logs;

namespace VietImage.Roles
{
    public class RegisteredUserRole
    {
        #region Attributes
        private User _userInfo;
        private int _id_Language;
        #endregion

        #region Properties
        public User UserInfo
        {
            get { return this._userInfo; }
            set { this._userInfo = value; }
        }

        public int ID_Language
        {
            get { return _id_Language; }
            set { _id_Language = value; }
        }
        #endregion

        #region Method
        public RegisteredUserRole()
        {
        }

        public bool Login(String username, String password)
        {
            this.UserInfo = User.ValidationUser(username, password);

            if (this.UserInfo == null)
                return false;

            VietImageDB_Log.WriteLog(this.UserInfo.Username + " logged in.");
            List<ErrorItem> errorList = LogRecentActivity.Insert(this.UserInfo.ID, 11, this.ID_Language);
            if (errorList != null)
            {
                VietImageDB_Log.WriteLog("Cannot write recent activities log.");
                VietImageDB_Log.WriteLog("Error list : ");
                foreach (ErrorItem kvp in errorList)
                    VietImageDB_Log.WriteLog("---" + kvp.Key + " error : " + kvp.Message);
            }
            else
                VietImageDB_Log.WriteLog("Recent activities log was written.");

            return true;
        }

        public void Logout()
        {
            List<ErrorItem> errorList = LogRecentActivity.Insert(this.UserInfo.ID, 12, this.ID_Language);
            if (errorList != null)
            {
                VietImageDB_Log.WriteLog("Cannot write recent activities log.");
                VietImageDB_Log.WriteLog("Error list : ");
                foreach (ErrorItem kvp in errorList)
                    VietImageDB_Log.WriteLog("---" + kvp.Key + " error : " + kvp.Message);
            }
            else
                VietImageDB_Log.WriteLog("Recent activities log was written.");

            VietImageDB_Log.WriteLog(this.UserInfo.Username + " logged out.");
        }

        public List<ErrorItem> ChangeUserInformation(User user, DetailUser du)
        {
            List<ErrorItem> errorList = User.ChangeInformation(user, this.UserInfo.ID, this.ID_Language);

            if (errorList == null)
            {
                if (du != null)
                {
                    errorList = DetailUser.ChangeInformation(du, this.UserInfo.ID, this.ID_Language);
                    if (errorList != null)
                    {
                        VietImageDB_Log.WriteLog("Cannot write users log.");
                        VietImageDB_Log.WriteLog("Error list : ");
                        foreach (ErrorItem kvp in errorList)
                            VietImageDB_Log.WriteLog("---" + kvp.Key + " error : " + kvp.Message);
                    }
                    else
                        VietImageDB_Log.WriteLog("Users log was written.");
                }
            }
            else
                VietImageDB_Log.WriteLog("User information cannot change.");

            return errorList;
        }

        public List<ErrorItem> ReceiveNoticeEmail(int id_Email, Boolean isChecked)
        {
            Dictionary<String, System.Object> search = new Dictionary<String, System.Object>();
            Dictionary<String, bool> exact = new Dictionary<String, bool>();

            search.Add("ReceivedEmail", this.UserInfo.Email);
            exact.Add("ReceivedEmail", true);
            search.Add("ID_Email", id_Email);

            NoticeEmail ne = NoticeEmail.Parse(NoticeEmail.GetNoticeEmail(search, exact, true));

            if (isChecked)
                ne.ID_Status = 1;
            else
                ne.ID_Status = 6;

            List<ErrorItem> errorList = NoticeEmail.ChangeInformation(ne, this.UserInfo.ID, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + "'s notice emails were changed");
            else
                VietImageDB_Log.WriteLog("Notice Email cannot change.");

            return errorList;
        }
        
        public List<Category> ViewListAvailableParentCategories()
        {
            List<Category> lc = Category.GetListAvailableParentCategories();
            return lc;
        }

        public List<Category> ViewListAvailableSubCategories()
        {
            List<Category> lc = Category.GetListAvailableSubCategories();
            return lc;
        }
        #endregion
    }
}